<?php
namespace App\Libs\Common;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/*
*   上上签电子合同
*/

class SignatureController extends Controller
{	
	// 测试地址
	private $url_cz='https://openapi.bestsign.info/openapi/v3';

	//正式地址
	private $url_sc='https://openapi.bestsign.cn/openapi/v3/';	

	private $developerId="1856166817215545953";

	private $signType="rsa";

	/**
	 * 获取合同地址
	 * @return [type] [description]
	 */
	public function getPreviewURL()
	{	
		$body['account']='15738457052';
		$body['contractId']='1915746369302168073';

		$path='contract/getPreviewURL/';

		$res=$this->returnUrl($path,json_encode($body));

	 	print_r($res);

	 	// exit();
	}

	//合同串联
	public function SeriesContract($order_id,$exp_number)
	{	
		header("Content-type: text/html; charset=utf-8");
		if (empty($order_id)) die(json_error('参数不能为空'));
		$row=DB::table("orders")->where("order_id",$order_id)->first();
		$row=ob_to_array($row);
		$goods=DB::table("goods")->where("goods_id",$row['goods_id'])->select("goods_date_due","goods_official_price")->first();
		if(empty($goods)) die(json_error('订单商品不存在'));
		$goods=ob_to_array($goods);
		$mobile=$row["user_phone"];//用户手机号(从订单表获取)
		$name=$row["user_name"];//用户姓名（从订单表获取）

		$reg_res=$this->reg($mobile,$name);
		if ($reg_res !== true) renderJson(1,"生成合同失败1");
		
		$account=$mobile;
		$identity=$row["user_id_number"];//证件号(从订单表获取)
		$identityType='0';//证件类型
		$contactMail='';//邮箱
		$contactMobile=$mobile;//联系手机
		$province='';//省
		$city='';//市
		$address=$row["user_province"].$row["user_address"];//地址  (从订单表获取)

		$setPer_res=$this->setPersonalCredential($account,$identity,$identityType,$contactMail,$contactMobile,$name,$province,$city,$address);
		if ($setPer_res !== true) die(json_error('生成合同失败2'));
		

		$applyCert_res=$this->applyCert($account);
		if ($applyCert_res !== true)die(json_error('生成合同失败3'));

		$signature_res=$this->signatureImage($account);
		if ($signature_res !== true) die(json_error('生成合同失败4'));

		$order_comefrom=$row["order_comefrom"];
		$fid=$this->storage($order_comefrom);
		if ($fid == '') die(json_error('生成合同失败5'));

		$add_address=$address;//地址  (从订单表获取)
		$goods_name=$row["goods_name"];//商品名称(从订单表获取)
		$goods_order_attr=mb_substr($row["goods_order_attr"],3,10,'utf-8');;//商品名称(商品属性：下单时所选属性 (字符串拼接))
		$exp_number=$exp_number;//快递单号(从订单表获取)
		$order_monthly_rent=$row["order_monthly_rent"];//月租金(从订单表获取)
		$order_tenancy_term=$row["order_tenancy_term"];//租期(从订单表获取)
		$order_gross_rent=$row["order_gross_rent"];//总租金(从订单表获取)
		$goods_official_price=$goods["goods_official_price"];//官方价格(从商品表获取)
		$create_time=date("Y年m月d日",time());//签约时间
		$zm_credit_score=$row["zm_credit_score"];//芝麻信用分（从订单表获取）
		// echo $exp_number.'||'.$order_monthly_rent.'||'.$order_tenancy_term.'||'.$order_gross_rent.'||'.$create_time.'||'.$zm_credit_score;exit();
		if($order_comefrom == "H5_JD")
		{
			$fids=$this->addPDFElements_jd($fid,$name,$identity,$account,$add_address,$goods_name,$goods_order_attr,$exp_number,$goods["goods_date_due"],$order_monthly_rent,$order_tenancy_term,$order_gross_rent,$goods_official_price,$create_time,$zm_credit_score);
		}else{
			$fids=$this->addPDFElements($fid,$name,$identity,$account,$add_address,$goods_name,$goods_order_attr,$exp_number,$goods["goods_date_due"],$order_monthly_rent,$order_tenancy_term,$order_gross_rent,$goods_official_price,$create_time,$zm_credit_score);
		}
		
		if ($fids == '') die(json_error('生成合同失败'));

		$contractId=$this->contract($fids);
		if ($contractId == '')die(json_error('创建合同失败1'));

		$addSigner_res=$this->addSigner($contractId,$mobile);
		if ($addSigner_res !== true)die(json_error('创建合同失败2'));


		$addCompany_res=$this->addCompany($contractId);
		if ($addCompany_res !== true) die(json_error('创建合同失败3'));


		$setSigner_res=$this->setSignerConfig($contractId,$account);
		if ($setSigner_res !== true) die(json_error('创建合同失败4'));

		$setCompany_res=$this->setCompany($contractId);
		if ($setCompany_res !== true)die(json_error('创建合同失败5'));

		$signCert_res=$this->signCert($contractId,$account,$order_comefrom);
		if ($signCert_res !== true) die(json_error('创建合同失败6'));

	 	$CompanyCert_res=$this->CompanyCert($contractId,$order_comefrom);
	 	
		if ($CompanyCert_res !== true) die(json_error('创建合同失败7'.$CompanyCert_res));

		$finish_res=$this->finish($contractId);
		if ($finish_res !== true)
		{
            die(json_error('创建合同失败'));
		}else{
			$res=DB::table("orders")->where("order_id",$order_id)->update(["electronic_contract_number"=>$contractId]);
			if ($res) {
				return true;
			}else{
				return false;
			}
		}
	}

    /**
	 * 1.  注册用户
	 * 返回值 241208 注册过的用户
	 */
	public function reg($mobile,$name)
	{
		header("Content-type: text/html; charset=utf-8");
		
		$body['mail']='';
		$body['account']="$mobile";//用户账号
		$body['mobile']=$mobile;
		$body['name']=$name;
		$body['userType']='1';

		$path='user/reg/';

		$res=$this->returnUrl($path,json_encode($body,JSON_UNESCAPED_UNICODE));

		$result=json_decode($res,true);
		
		if ($result['errno'] == '0' || $result['errno'] == '241208') 
		{  
			return true;
		}else{

			return "用户注册:".$result['errmsg'];
		}
	 	
	}

	/**
	 * 2.  为某一个个人用户设置证件信息，该证件信息用于申请数字证书 
	 *  // 返回值 241308  证书已经存在
	 */
	public function setPersonalCredential($account,$identity,$identityType,$contactMail,$contactMobile,$name,$province,$city,$address)
	{

		$path='user/setPersonalCredential/';
		$body['account']=$account;
		$body['identity']=$identity;
		$body['identityType']=$identityType;
		$body['contactMail']=$contactMail;
		$body['contactMobile']=$contactMobile;
		$body['name']=$name;
		$body['province']=$province;
		$body['city']=$city;
		$body['address']=$address;

		$res=$this->returnUrl($path,json_encode($body));
		
		$result=json_decode($res,true);

		if ($result['errno'] == '0' || $result['errno'] == '241308') 
		{
			return true;
		}else{
			
			return  "为某一个用户设置证件信息:".$result['errmsg'];
		}
		// /240006
	 	// /print_r($res);
	}


	/**
	 *  3 用户申请数字证书
	 *  返回值 241308 证书已经存在 
	 */
	public function applyCert($account)
	{
		$path='user/applyCert/';

		// $body='{"account":"15738457052",
		// 		"certType":"CFCA"
		// 	  }';

		$body["account"]=$account;
		$body["certType"]='CFCA';
		$res=$this->returnUrl($path,json_encode($body));
		
		$result=json_decode($res,true);

		if ($result['errno'] == '0' || $result['errno'] == '241308') 
		{
			return true;
		}else{
			
			return  "用户申请数字证书:".$result['errmsg'];
		}
		print_r($res);
	}



	/**
	 * 4.  创建用户签名/印章图片
	 *
	 */
	public function signatureImage($account)
	{
		$path='signatureImage/user/create/';

		$body["account"]=$account;
		$body["text"]='';		
		$res=$this->returnUrl($path,json_encode($body));
		
		$result=json_decode($res,true);

		if ($result['errno'] == '0') 
		{
			return true;
		}else{
			
			return  "创建用户签名/印章图片:".$result['errmsg'];
		}

	
	}


	/**
	 * 5.  上传合同  
	 */
	public function storage($order_comefrom)
	{ 	
		$path='storage/upload/';

		if ($order_comefrom == "H5_JD") 
		{
			$file_path="./JDeightmonth.pdf";
		}else{
			$file_path="./eightmonth.pdf";
		}
		

		$body["account"]="guoshengnan@lvyuechupin.com";//固定参数 
		$connet=file_get_contents($file_path);
		$body["fdata"] = base64_encode($connet);
		
		$body["fmd5"]=md5($connet);

		$body["ftype"]="PDF";//固定参数
		$body["fname"]="eightmonth.pdf";
		$body["fpages"]="4";//固定参数

		$res=$this->returnUrl($path,json_encode($body));

		$result=json_decode($res,true);
		//print_r($result);exit();
		if ($result['errno'] == '0') 
		{	
			return $result["data"]['fid'];
		}else{
			
			return  "上传合同:".$result['errmsg'];
		}
	}


	/**
	 * 6.  为 合同（PDF 文件） 添加元素
	 */
	public function addPDFElements($fid,$name,$idCard,$account,$address,$goods_name,$goods_order_attr,$exp_number,$goods_date_due,$order_monthly_rent,$order_tenancy_term,$order_gross_rent,$goods_official_price,$create_time,$zm_credit_score)
	{
		$path='storage/addPDFElements/';

		$body["account"]="guoshengnan@lvyuechupin.com";//固定参数 
		$body["fid"]=$fid;//上传合同返回的文件编号
		$body["elements"]=array(array("x"=>'0.2272',
						"y"=>'0.1989',
						"fontSize"=>'12',
						"type"=>'text',
						"pageNum"=>'1',
						"value"=>$name
						),
						
						array("x"=>'0.2586',
							"y"=>'0.2175',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$idCard
						),

						array("x"=>'0.2586',
							"y"=>'0.236',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$account
						),

						array("x"=>'0.2586',
							"y"=>'0.2545',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$address
						),

						// array("x"=>'0.1512',
						// 	"y"=>'0.4306',
						// 	"fontSize"=>'12',
						// 	"type"=>'text',
						// 	"pageNum"=>'1',
						// 	"value"=>'品牌：'.$goods_name
						// ),

						// array("x"=>'0.4741',
						// 	"y"=>'0.4306',
						// 	"fontSize"=>'12',
						// 	"type"=>'text',
						// 	"pageNum"=>'1',
						// 	"value"=>'颜色：'.$goods_order_attr
						// ),

						// array("x"=>'0.1512',
						// 	"y"=>'0.4491',
						// 	"fontSize"=>'12',
						// 	"type"=>'text',
						// 	"pageNum"=>'1',
						// 	"value"=>'型号：'.$goods_name
						// ),

						// array("x"=>'0.4743',
						// 	"y"=>'0.4491',
						// 	"fontSize"=>'12',
						// 	"type"=>'text',
						// 	"pageNum"=>'1',
						// 	"value"=>'快递单号：'.$exp_number
						// ),

						array("x"=>'0.1512',
							"y"=>'0.4306',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>'商品名称：'.$goods_name
						),

						array("x"=>'0.1512',
							"y"=>'0.4491',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>'规格颜色：'.$goods_order_attr //规格参数
						),
						array("x"=>'0.1512',
							"y"=>'0.4676',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>'快递单号：'.$exp_number
						),

						array("x"=>'0.7253',
							"y"=>'0.6066',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'2',
							"value"=> $goods_date_due //多久归用户所有
						),
						array("x"=>'0.6608',
							"y"=>'0.6807',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'3',
							"value"=>  $goods_date_due//多久归用户所有
						),

						// array("x"=>'0.4852',
						// 	"y"=>'0.4676',
						// 	"fontSize"=>'12',
						// 	"type"=>'text',
						// 	"pageNum"=>'1',
						// 	"value"=>$goods_name
						// ),


						array("x"=>'0.436',
							"y"=>'0.5325',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$order_monthly_rent //月租金
						),

						array("x"=>'0.7122',
							"y"=>'0.5325',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$order_tenancy_term//租期
						),

						array("x"=>'0.205',
							"y"=>'0.551',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$order_gross_rent//总租金
						),

						array("x"=>'0.6908',
							"y"=>'0.551',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$goods_official_price//官方价格
						),

						array("x"=>'0.2701',
							"y"=>'0.8938',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'4',
							"value"=>$create_time//签约时间(时间一样)
						),

						array("x"=>'0.7303',
							"y"=>'0.8938',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'4',
							"value"=>$create_time//签约时间(时间一样)
						),

						array("x"=>'0.4038',
							"y"=>'0.6344',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$zm_credit_score//芝麻信用分
						),
					
				  );
		//print_r($body);exit();
		$res=$this->returnUrl($path,json_encode($body));

		$result=json_decode($res,true);
		
		if ($result['errno'] == '0') 
		{	
			return $result["data"]['fid'];
		}else{
			
			return  "为合同(PDF文件):".$result['errmsg'];
		}
	}


	/**
	 * 6.  为 京东 合同（PDF 文件） 添加元素
	 */
	public function addPDFElements_jd($fid,$name,$idCard,$account,$address,$goods_name,$goods_order_attr,$exp_number,$goods_date_due,$order_monthly_rent,$order_tenancy_term,$order_gross_rent,$goods_official_price,$create_time,$zm_credit_score)
	{
		$path='storage/addPDFElements/';

		$body["account"]="guoshengnan@lvyuechupin.com";//固定参数 
		$body["fid"]=$fid;//上传合同返回的文件编号
		$body["elements"]=array(array("x"=>'0.2218',
						"y"=>'0.1976',
						"fontSize"=>'12',
						"type"=>'text',
						"pageNum"=>'1',
						"value"=>$name
						),
						
						array("x"=>'0.2521',
							"y"=>'0.2161',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$idCard
						),

						array("x"=>'0.2521',
							"y"=>'0.2347',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$account
						),

						array("x"=>'0.2521',
							"y"=>'0.2532',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$address
						),

						// array("x"=>'0.1512',
						// 	"y"=>'0.4306',
						// 	"fontSize"=>'12',
						// 	"type"=>'text',
						// 	"pageNum"=>'1',
						// 	"value"=>'品牌：'.$goods_name
						// ),

						// array("x"=>'0.4741',
						// 	"y"=>'0.4306',
						// 	"fontSize"=>'12',
						// 	"type"=>'text',
						// 	"pageNum"=>'1',
						// 	"value"=>'颜色：'.$goods_order_attr
						// ),

						// array("x"=>'0.1512',
						// 	"y"=>'0.4491',
						// 	"fontSize"=>'12',
						// 	"type"=>'text',
						// 	"pageNum"=>'1',
						// 	"value"=>'型号：'.$goods_name
						// ),

						// array("x"=>'0.4743',
						// 	"y"=>'0.4491',
						// 	"fontSize"=>'12',
						// 	"type"=>'text',
						// 	"pageNum"=>'1',
						// 	"value"=>'快递单号：'.$exp_number
						// ),

						array("x"=>'0.1513',
							"y"=>'0.4458',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>'商品名称：'.$goods_name
						),

						array("x"=>'0.1513',
							"y"=>'0.4643',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>'规格颜色：'.$goods_order_attr //规格参数
						),
						array("x"=>'0.1513',
							"y"=>'0.4828',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>'快递单号：'.$exp_number
						),

						array("x"=>'0.6986',
							"y"=>'0.6093',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'2',
							"value"=> $goods_date_due //多久归用户所有
						),
						array("x"=>'0.6303',
							"y"=>'0.6829',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'3',
							"value"=>  $goods_date_due//多久归用户所有
						),

						// array("x"=>'0.4852',
						// 	"y"=>'0.4676',
						// 	"fontSize"=>'12',
						// 	"type"=>'text',
						// 	"pageNum"=>'1',
						// 	"value"=>$goods_name
						// ),


						array("x"=>'0.4169',
							"y"=>'0.549',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$order_monthly_rent //月租金
						),

						array("x"=>'0.6474',
							"y"=>'0.549',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$order_tenancy_term//租期
						),

						array("x"=>'0.2888',
							"y"=>'0.5681',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$order_gross_rent//总租金
						),

						array("x"=>'0.1831',
							"y"=>'0.586',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$goods_official_price//官方价格
						),

						array("x"=>'0.2521',
							"y"=>'0.8925',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'4',
							"value"=>$create_time//签约时间(时间一样)
						),

						array("x"=>'0.7261',
							"y"=>'0.8925',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'4',
							"value"=>$create_time//签约时间(时间一样)
						),

						array("x"=>'0.3529',
							"y"=>'0.664',
							"fontSize"=>'12',
							"type"=>'text',
							"pageNum"=>'1',
							"value"=>$zm_credit_score//芝麻信用分
						),
					
				  );
		//print_r($body);exit();
		$res=$this->returnUrl($path,json_encode($body));

		$result=json_decode($res,true);
		
		if ($result['errno'] == '0') 
		{	
			return $result["data"]['fid'];
		}else{
			
			return  "为合同(PDF文件):".$result['errmsg'];
		}
	}



	/**
	 * 7.  创建合同
	 */
	public function contract($fid)
	{
		$path='contract/create/';

		$body["account"]="guoshengnan@lvyuechupin.com";//固定参数
		$body["fid"]=$fid;//第6步返回的 文件编号
		$body["expireTime"]=time()+604800;//合同过期时间 当前时间加7天
		$body["title"]='租赁合同';//固定参数
		$body["description"]='';

		$res=$this->returnUrl($path,json_encode($body));

		$result=json_decode($res,true);
		//print_r($result);exit();
		if ($result['errno'] == '0') 
		{	
			return $result["data"]['contractId'];
		}else{
			
			return  "创建合同:".$result['errmsg'];
		}
		
	}



	/**
	 * 8.  添加合同签署者(用户)  
	 */
	public function addSigner($contractId,$signer)
	{
		$path='contract/addSigner/';

		$body["contractId"]=$contractId;//第七步返回的 合同编号
		$body["signer"]=$signer;//要添加的签署者 账号

		$res=$this->returnUrl($path,json_encode($body));

		$result=json_decode($res,true);

		if ($result['errno'] == '0') 
		{	
			return true;
		}else{
			
			return  "添加合同签署者(用户):".$result['errmsg'];
		}
	}


	/**
	 * 9.  添加合同签署者(企业)
	 */
	public function addCompany($contractId)
	{
		$path='contract/addSigner/';

		$body["contractId"]=$contractId;//第七步返回的合同编号
		$body["signer"]='guoshengnan@lvyuechupin.com';//要添加的签署者 账号

		$res=$this->returnUrl($path,json_encode($body));

		$result=json_decode($res,true);

		if ($result['errno'] == '0') 
		{	
			return true;
		}else{
			
			return  "添加合同签署者(企业):".$result['errmsg'];
		}
	}



	/**
	 * 10.  设置合同签署参数(用户) 
	 */
	public function setSignerConfig($contractId,$account)
	{
		$path='contract/setSignerConfig/';

		$body["contractId"]=$contractId;//第七步返回的合同编号
		$body["account"]=$account;//用户手机号
		$body["signaturePositions"]='';
		$body["isAllowChangeSignaturePosition"]='';
		$body["returnUrl"]='';
		$body["password"]='';
		$body["isVerifySigner"]='';
		$body["vcodeMail"]='';
		$body["vcodeMobile"]='';
		$body["isDrawSignatureImage"]='';
		$body["signatureImageName"]='';
		$body["certType"]='CFCA';
		
		$res=$this->returnUrl($path,json_encode($body));

		$result=json_decode($res,true);

		if ($result['errno'] == '0') 
		{	
			return true;
		}else{
			
			return  "设置合同签署参数(用户):".$result['errmsg'];
		}
	}


	/**
	 * 11.  设置合同签署参数(企业)
	 */
	public function setCompany($contractId)
	{
		$path='contract/setSignerConfig/';

		$body["contractId"]=$contractId;//第七步返回的合同编号
		$body["account"]='guoshengnan@lvyuechupin.com';//公司账户
		$body["signaturePositions"]='';
		$body["isAllowChangeSignaturePosition"]='';
		$body["returnUrl"]='';
		$body["password"]='';
		$body["isVerifySigner"]='';
		$body["vcodeMail"]='';
		$body["vcodeMobile"]='';
		$body["isDrawSignatureImage"]='';
		$body["signatureImageName"]='xinhetongzhang.png';//固定参数
		$body["certType"]='CFCA';
		
		$res=$this->returnUrl($path,json_encode($body));

		$result=json_decode($res,true);

		if ($result['errno'] == '0') 
		{	
			return true;
		}else{
			
			return  "设置合同签署参数(企业):".$result['errmsg'];
		}
	}


	/**
	 * 12. 签署合同（用户）
	 */
	public function signCert($contractId,$signer,$order_comefrom)
	{
		$path='contract/sign/cert/';

		$body["contractId"]=$contractId;	
		$body["signer"]=$signer;
		
		if($order_comefrom == "H5_JD")
		{
			$body["signaturePositions"]=array(array('x'=>'0.6992','y'=>'0.8534','pageNum'=>'4'));
		}else{
			$body["signaturePositions"]=array(array('x'=>'0.6658','y'=>'0.8197','pageNum'=>'4'));
		}
			
		$res=$this->returnUrl($path,json_encode($body));

		$result=json_decode($res,true);

		if ($result['errno'] == '0') 
		{	
			return true;
		}else{
			
			return  "签署合同（用户）:".$result['errmsg'];
		}
	}

	/**
	 * 12. 签署合同（企业）
	 */
	public function CompanyCert($contractId,$order_comefrom)
	{
		$path='contract/sign/cert/';

		$body["contractId"]=$contractId;	
		$body["signer"]='guoshengnan@lvyuechupin.com';//公司账户	
		
		if($order_comefrom == "H5_JD")
		{
			$body["signaturePositions"]=array(array('x'=>'0.2436','y'=>'0.7544','pageNum'=>'4'));
		}else{
			$body["signaturePositions"]=array(array('x'=>'0.2369','y'=>'0.7641','pageNum'=>'4'));
		}	
		$res=$this->returnUrl($path,json_encode($body));

		$result=json_decode($res,true);

		if ($result['errno'] == '0') 
		{	
			return true;
		}else{
			
			return  "签署合同（企业）:".$result['errmsg'];
		}
	}

	/**
	 * 13. 结束合同   
	 */
	public function finish($contractId)
	{
		$path='contract/finish/';

		$body["contractId"]=$contractId;
		$res=$this->returnUrl($path,json_encode($body));

		$result=json_decode($res,true);

		if ($result['errno'] == '0') 
		{	
			return true;
		}else{
			
			return  "结束合同:".$result['errmsg'];
		}
	}

	// 下载合同
	public function getDownloadURLs()
	{

		$contractId= request('contractId');

		$path='contract/getDownloadURLs/';

		$body="{
				'contractId': $contractId
			}";//合同编号

		$res=$this->returnUrl($path,$body);

		print_r($res);
	}


    //调用
	public function returnUrl($path,$body)
	{	

		$param['developerId']=$this->developerId;
		$param['signType']=$this->signType;
		$param['rtick']=time().snMaker(4);

		$md5_body=md5($body);

		$param['sign']=$this->returnSignString($path,$param['rtick'],$md5_body);

		$url=$this->url_sc.$path.'?';


        ksort($param);

        $str = "";

        foreach ($param as $k => $v) 
        {  
			$str.="$k=".$v.'&';
        }

        $url.=trim($str,'&');

        $res=$this->request_post($url,$body);

        return $res;

	}

	// 返回拼接串
	private function returnSignString($post_URL,$rtick,$body_md5)
	{	
		$signString="developerId=".$this->developerId."rtick=".$rtick."signType=".$this->signType."/openapi/v3/".$post_URL.$body_md5;
        $RsaUtil= new RsaUtil();
		$sign=$RsaUtil::sign($signString,"./rsa/rsa_shangshangqian_private_key.pem");

		return urlEncode($sign);
	}


	/**
	 * 模拟POST请求
	 * @param  string $url   [请求地址]
	 * @param  string $param [参数]
	 */
	 private function request_post($url = '', $param = '') 
     {		

            $ch = curl_init();//初始化curl

            curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页
            curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
            curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
            curl_setopt($ch, CURLOPT_POSTFIELDS,$param);
            curl_setopt($ch, CURLOPT_HTTPHEADER,array(
                    'Content-Type: application/json; charset=utf-8',
                ));

            $data = curl_exec($ch);//运行curl

            if($data === false)
            {
            	
                return  'Curl error: ' . curl_error($ch);
            }
            
            curl_close($ch);
            
            return $data;
      }


	//郑投网 传用户和订单信息
	// public function ztw_order()
	// {	
	// 	$path='http://117.158.208.96:10102/thirdPerson/personalInfo';

	// 	$body["contractId"]=$contractId;
	// 	$res=$this->returnUrl($path,json_encode($body));

	// 	$result=json_decode($res,true);

	// 	if ($result['errno'] == '0') 
	// 	{	
	// 		return true;
	// 	}else{
			
	// 		return  "结束合同:".$result['errmsg'];
	// 	}	
	// }



	
	


}